﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Passwords.Models;
using Passwords.Utils;
using System.Windows.Input;

namespace Passwords.ViewModels
{
    public class PasswordGenViewModel : ViewModelBase, IDisposable
    {
        public PasswordGenViewModel()
            : base()
        {
            BindCommands();
        }

        public void Dispose()
        {
            UnbindCommands();
            GC.SuppressFinalize(this);
        }

        ~PasswordGenViewModel()
        {
            Dispose();
        }


        private string password;
        private bool useLowerCase = true;
        private bool userUpperCase = true;
        private bool useDigits = true;
        private bool useSpecial = false;
        private double strength;
        private int numChars = 10;
        private FieldContract field;
        private string strengthText = "weak";

        public string StrengthText
        {
            get { return strengthText; }
            set
            {
                if (strengthText != value)
                {
                    strengthText = value;
                    OnPropertyChanged("StrengthText");
                }
            }
        }


        public FieldContract Field
        {
            get { return field; }
            set
            {
                if (field != value)
                {
                    field = value;
                    OnPropertyChanged("Field");
                }
            }
        }


        public int NumChars
        {
            get { return numChars; }
            set
            {
                if (numChars != value)
                {
                    numChars = value;
                    OnPropertyChanged("NumChars");
                }
            }
        }


        public double Strength
        {
            get { return strength; }
            set
            {
                if (strength != value)
                {
                    strength = value;
                    OnPropertyChanged("Strength");
                    StrengthText = PasswordUtility.GetLevelFromValue((int)value).ToString();
                }
            }
        }


        public bool UseSpecial
        {
            get { return useSpecial; }
            set
            {
                if (useSpecial != value)
                {
                    useSpecial = value;
                    OnPropertyChanged("UseSpecial");
                }
            }
        }


        public bool UseDigits
        {
            get { return useDigits; }
            set
            {
                if (useDigits != value)
                {
                    useDigits = value;
                    OnPropertyChanged("UseDigits");
                }
            }
        }


        public bool UseUpperCase
        {
            get { return userUpperCase; }
            set
            {
                if (userUpperCase != value)
                {
                    userUpperCase = value;
                    OnPropertyChanged("UseUpperCase");
                }
            }
        }


        public bool UseLowerCase
        {
            get { return useLowerCase; }
            set
            {
                if (useLowerCase != value)
                {
                    useLowerCase = value;
                    OnPropertyChanged("UseLowerCase");
                }
            }
        }


        public string Password
        {
            get { return password; }
            set
            {
                if (password != value)
                {
                    password = value;
                    OnPropertyChanged("Password");
                }
            }
        }

        protected override void OnPropertyChanged(string propertyName)
        {
            if (propertyName != "Strength")
            {
                Strength = PasswordUtility.GetStrength(password);
            }
            base.OnPropertyChanged(propertyName);
        }

        public void GeneratePassword()
        {
            Complexity complexity = Complexity.None;
            if (useDigits) complexity |= Complexity.Digits;
            if (useLowerCase) complexity |= Complexity.Lowercase;
            if (UseUpperCase) complexity |= Complexity.Uppercase;
            if (useSpecial) complexity |= Complexity.Complex;
            Password = PasswordUtility.GeneratePassword(complexity, numChars);
        }

        private void BindCommands()
        {
            BindCommand(Commands.GeneratePassword, GeneratePasswordCommand);
        }

        private void GeneratePasswordCommand(object sender, ExecutedRoutedEventArgs e)
        {
            e.Handled = true;
            GeneratePassword();
        }

    }
}
